package com.fone.player.activity.personal.adapter;

import java.util.List;

import android.app.Activity;
import android.database.DataSetObserver;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.activity.personal.PlayRecordFragment;
import com.fone.player.cache.ImageDownloadModule;
import com.fone.player.entity.PlayRecord;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.fone.player.util.ScreenUtil;
import com.fone.player.view.QuickPlayMoreOperationPopWindow;
import com.fone.player.view.QuickPlayMoreOperationPopWindow.IMoreOperationListener;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

public class QuickPlayAdapter extends BaseAdapter {
	private String TAG = "QuickPlayAdapter";
	private List<PlayRecord> mHistoryVideos;
	private Activity mActivity;
	private int width = 0;
	private int height = 0;
	private Handler mHandler;
	private DisplayImageOptions options;

	public QuickPlayAdapter(Activity activity, List<PlayRecord> list,
			Handler handler) {
		this.mActivity = activity;
		this.mHistoryVideos = list;
		width = ScreenUtil.dp2px(140.0f);
		height = ScreenUtil.dp2px(80.0f);
		this.mHandler = handler;
		this.options = FoneUtil
				.createDisplayImageOptionsByDrawableId(R.drawable.default_224_128);
	}

	@Override
	public int getCount() {
		int count = 0;
		if (mHistoryVideos != null) {
			count = mHistoryVideos.size();
		}
		return count;

	}

	@Override
	public Object getItem(int position) {
		return mHistoryVideos.get(position);

	}

	@Override
	public long getItemId(int position) {
		return position;

	}

	@Override
	public View getView(final int position, View convertView,
			final ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(mActivity).inflate(
					R.layout.quick_play_list_item, null);
			viewHolder.iv_pic = (ImageView) convertView
					.findViewById(R.id.iv_quick_play_pic);
			viewHolder.iv_more_operation = (ImageView) convertView
					.findViewById(R.id.iv_more_operation);
			viewHolder.tv_local = (TextView) convertView
					.findViewById(R.id.tv_quick_play_local);
			viewHolder.tv_name = (TextView) convertView
					.findViewById(R.id.tv_quick_play_name);
			viewHolder.tv_time = (TextView) convertView
					.findViewById(R.id.tv_quick_paly_item_time);
			viewHolder.pb = (ProgressBar) convertView
					.findViewById(R.id.pb_quick_play_item_progressbar);
			viewHolder.needInflate = false;
			convertView.setTag(viewHolder);
		} else if (((ViewHolder) convertView.getTag()).needInflate) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(mActivity).inflate(
					R.layout.quick_play_list_item, null);
			viewHolder.iv_pic = (ImageView) convertView
					.findViewById(R.id.iv_quick_play_pic);
			viewHolder.iv_more_operation = (ImageView) convertView
					.findViewById(R.id.iv_more_operation);
			viewHolder.tv_local = (TextView) convertView
					.findViewById(R.id.tv_quick_play_local);
			viewHolder.tv_name = (TextView) convertView
					.findViewById(R.id.tv_quick_play_name);
			viewHolder.tv_time = (TextView) convertView
					.findViewById(R.id.tv_quick_paly_item_time);

			viewHolder.pb = (ProgressBar) convertView
					.findViewById(R.id.pb_quick_play_item_progressbar);
			viewHolder.needInflate = false;
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final PlayRecord itemBean = mHistoryVideos.get(position);
		viewHolder.tv_name.setText(itemBean.getPlayRecordName());

		long min = (itemBean.getPlayRecordAlreadyPlayTime() / 1000) / 60;
		long sencond = (itemBean.getPlayRecordAlreadyPlayTime() / 1000) % 60;
		if (min != 0) {

			if (sencond < 10) {
				viewHolder.tv_time.setText("观看至" + min + "分0" + sencond + "秒");
			} else {
				viewHolder.tv_time.setText("观看至" + min + "分" + sencond + "秒");
			}
		} else {
			viewHolder.tv_time.setText("观看至" + sencond + "秒");
		}
		if (itemBean.getPlayRecordType() == PlayRecord.RECORD_PLAYER_LOCAL
				|| itemBean.getPlayRecordType() == PlayRecord.RECORD_PLAYER_CACHE) {
			viewHolder.tv_local.setVisibility(View.VISIBLE);
		} else {
			viewHolder.tv_local.setVisibility(View.GONE);
		}
		viewHolder.iv_pic.setImageBitmap(null);
		viewHolder.iv_pic.setImageDrawable(null);
		if (TextUtils.isEmpty(itemBean.getPlayRecordImageUrl())) {
			viewHolder.iv_pic.setImageBitmap(null);
			viewHolder.iv_pic.setImageDrawable(null);
			L.i(TAG, "itemBean.pic:" + itemBean.getPlayRecordImageUrl());
		} else {
			if (itemBean.getPlayRecordType() == PlayRecord.RECORD_PLAYER_LOCAL) {

				ImageDownloadModule.getInstance().display(
						itemBean.getPlayRecordImageUrl(), width, height,
						viewHolder.iv_pic);
			} else {
				ImageDownloadModule.getInstance().download(
						itemBean.getPlayRecordImageUrl(), viewHolder.iv_pic,
						options);
			}
		}
		int progress = 0;
		if (itemBean.getPlayRecordTotalTime() != 0) {
			progress = (int) ((float) itemBean.getPlayRecordAlreadyPlayTime()
					/ (float) itemBean.getPlayRecordTotalTime() * 100);
		}
		viewHolder.pb.setProgress(progress);
		final View view = convertView;
		viewHolder.iv_more_operation.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showMoreOperation(v, view, position, itemBean);
				((ImageView) v)
						.setImageResource(R.drawable.quick_more_small_click);
			}
		});
		return convertView;
	}

	public static class ViewHolder {
		public ImageView iv_pic;
		public ImageView iv_more_operation;
		public TextView tv_local;
		public TextView tv_name;
		public TextView tv_time;
		public ProgressBar pb;
		public boolean needInflate;

	}

	private void showMoreOperation(final View view, final View convertView,
			final int position, PlayRecord record) {

		boolean isLocal = false;
		if (record.getPlayRecordType() == PlayRecord.RECORD_PLAYER_LOCAL
				|| record.getPlayRecordType() == PlayRecord.RECORD_PLAYER_CACHE) {
			isLocal = true;
		} else if (record.getPlayRecordType() == PlayRecord.RECORD_PLAYER_NETWORK
				&& TextUtils.isEmpty(record.getPlayRecordShareUrl())) {
			isLocal = true;
		}
		QuickPlayMoreOperationPopWindow popWindow = new QuickPlayMoreOperationPopWindow(
				mActivity, isLocal);
		final int index = position;
		popWindow.setOperationListener(new IMoreOperationListener() {

			@Override
			public void share() {
				Message message = mHandler.obtainMessage();
				message.what = PlayRecordFragment.SHARE;
				message.arg1 = position;
				mHandler.sendMessage(message);
			}

			@Override
			public void getDetail() {
				Message message = mHandler.obtainMessage();
				message.what = PlayRecordFragment.DETAIL;
				message.arg1 = position;
				mHandler.sendMessage(message);
			}

			@Override
			public void delete() {
				Message msg = mHandler.obtainMessage();
				msg.arg1 = index;
				msg.obj = convertView;
				msg.what = PlayRecordFragment.DELETE;
				mHandler.sendMessage(msg);
			}

			@Override
			public void collect() {
				Message message = mHandler.obtainMessage();
				message.what = PlayRecordFragment.COLLECT;
				message.arg1 = index;
				mHandler.sendMessage(message);

			}

			@Override
			public void dismiss() {
				((ImageView) view)
						.setImageResource(R.drawable.quick_more_small);
			}
		});
		if (record.getPlayRecordType() == PlayRecord.RECORD_PLAYER_LOCAL) {
			popWindow.showPopupWindow(view, true);
		} else {
			popWindow.showPopupWindow(view, false);
		}
	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {
		L.i(TAG, "observer observer observer:"+observer);
		if (observer != null) {
			super.unregisterDataSetObserver(observer);
		}

	}
}
